package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Vector3d;
import mygame.components.PositionComp;
import mygame.utils.PositionSyncer;

/**
 * test
 * @author normenhansen
 */
public class Main extends SimpleApplication implements CollisionListener{

    private CollisionWorld world;
    private static Main app;
    private CollisionObject oDynamic, oPlayer;
    private Material mat1, mat2;
    private PositionSyncer syncer = new PositionSyncer();
    
    public static void main(String[] args) {
        app = new Main();
        app.start();
        //app.getContext().getSettings().setFrameRate(4);
        
    }

    @Override
    public void simpleInitApp() {

        mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Blue);
        mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat2.setColor("Color", ColorRGBA.White);
        
        // initiate collision detection
        world = new CollisionWorld();
        world.addListener(this);
        
        float size = 3f;
        
        oPlayer = new testEntity("player", size);
        CollisionObject oStatic = new testEntity("static1", size);
        ((testEntity)oPlayer).setPlayer(true);
        ((testEntity)oStatic).setStatic(true);
        
        oPlayer.setPosition(new PositionComp(-2,0,0));
        oStatic.setPosition(new PositionComp(4,0,0));
        
        world.addCollisionObject(oPlayer);
        world.addCollisionObject(oStatic);

        
        // create visual representation
        int samples = 16;
        Sphere sP = new Sphere(samples, samples, size);
        Sphere sS = new Sphere(samples, samples, size);
        Geometry geomP = new Geometry("PlayerSphere", sP);
        Geometry geomS = new Geometry("StaticSphere", sS);
        geomP.setMaterial(mat1);
        geomS.setMaterial(mat2);
        
        geomP.setLocalTranslation(-2, 0, 0);
        geomS.setLocalTranslation(4, 0, 0);
        
        rootNode.attachChild(geomP);
        rootNode.attachChild(geomS);
        
        // link collision object to visual object
        syncer.addPair(oPlayer, geomP);
        
    }

    float counter = 0;
    @Override
    public void simpleUpdate(float tpf) {
        double x = ((testEntity)oPlayer).getPosition().getX();
        counter += tpf;
        x += Math.sin(counter)*0.3;
        oPlayer.setPosition(new PositionComp(x,0,0));
        //System.out.println("tpf: "+tpf);
        mat1.setColor("Color", ColorRGBA.Green);
        syncer.sync();
        world.checkCollisions();
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    
    public void collisionDetected(CollisionResult result) {
        mat1.setColor("Color", ColorRGBA.Red);
        
    }
}
